package chapter07

import scala.math.Ordering

object Test18_ComplexWordCount {
  def main(args: Array[String]): Unit = {
    val tupleList: List[(String, Int)] = List(
      ("hello", 1),
      ("hello word", 2),
      ("hello scala", 3),
      ("hello spark form scala", 1),
      ("hello flink from scala", 2))

    val newStringList: List[String] = tupleList.map(
      kv => {
        (kv._1.trim + " ") * kv._2
      })
    println(newStringList)


    //2、接下来的操作和普通版本完全一致
    val tuples: List[(String, Int)] = newStringList.flatMap(
      _.split(" "))
      .groupBy(e => e)
      .map(kv => (
        kv._1, kv._2.size
      ))
      .toList
      .sortBy(_._2)(Ordering[Int].reverse)
      .take(3)

    println(tuples)



    //思路二、直接基于预统计的结果进行转换
    println(".........思路二...................")
    //1、将字符串打散为单词，并结合对应的个数包装成二元组
    val preCountList: List[(String, Int)] = tupleList.flatMap(
      tuple => {
        val strings: Array[String] = tuple._1.split(" ")
        strings.map(word => (
          word, tuple._2
        ))
      }
    )
    println("......................."+preCountList)

    //2.对二元组按照单词进行分组
    val preCountMap: Map[String, List[(String, Int)]] = preCountList.groupBy(tuple => tuple._1)
    println(preCountMap)


    //3、叠加每个单词预统计的个数值
    val countMap: Map[String, Int] = preCountMap.mapValues(
      tupleList => tupleList.map(_._2).sum
    )
    println("............"+countMap)

    //4、转换为list, 排序取前三
    val countToList = countMap.toList
      .sortWith(_._2 > _._2)
      .take(3)
    println(countToList)
  }


}
